package cn.edu.cuit.aumalls.service.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.cuit.aumalls.entity.CartGoods;
import cn.edu.cuit.aumalls.entity.OrderRecipients;
import cn.edu.cuit.aumalls.entity.SubmitOrderDetail;
import cn.edu.cuit.aumalls.entity.SubmitOrders;
import cn.edu.cuit.aumalls.service.CartIntentService;
import en.edu.cuit.aumalls.mapper.CartgoodsMapper;
import en.edu.cuit.aumalls.mapper.OrderRecipientsMapper;
import en.edu.cuit.aumalls.mapper.SubmitOrderDetailMapper;
import en.edu.cuit.aumalls.mapper.SubmitOrderMapper;

@Service("cartIntentService")
public class CartIntentServiceImpl implements CartIntentService {
	
	@Autowired
	OrderRecipientsMapper orderRecipientsMapper;
	
	@Autowired
	CartgoodsMapper cartgoodsMapper;
	
	@Autowired
	SubmitOrderMapper submitOrderMapper;
	
	@Autowired
	SubmitOrderDetailMapper submitOrderDetailMapper;
	
	@Override
	public List<OrderRecipients> showCartIntent(int recipients_user) {
		// TODO Auto-generated method stub
		return orderRecipientsMapper.findAll(recipients_user);
	}
	
	public List<CartGoods> showCartGoods(){
		return cartgoodsMapper.findAll();
	}
	
	public float showDiscount() {
		float discount = 0;
		List<CartGoods> cartGoodsList = cartgoodsMapper.findAll();
		
		for(int i=0;i<cartGoodsList.size();i++) {
			discount = discount + cartGoodsList.get(i).getGoods_price() - cartGoodsList.get(i).getReal_price();
		}
		
		return discount;
	}

	public float showTotalPrice() {
		float totalPrice = 0;
		List<CartGoods> cartGoodsList = cartgoodsMapper.findAll();
		
		for(int i=0;i<cartGoodsList.size();i++) {
			totalPrice = totalPrice + cartGoodsList.get(i).getTotal_price();
		}
		
		return totalPrice;
	}
	
	public double showDuty() {
		double goodsDuty = 0;
		List<CartGoods> cartGoodsList = cartgoodsMapper.findAll();
		
		for(int i=0;i<cartGoodsList.size();i++) {
			goodsDuty = goodsDuty  + cartGoodsList.get(i).getGoods_duty();
		}
		
		return goodsDuty;
	}

	@Override
	public int addRecipient(OrderRecipients orderRecipients) {
		// TODO Auto-generated method stub
		return orderRecipientsMapper.insertOrderRecipients(orderRecipients);
	}

	@Override
	public int submit(double totalprice ,int userId) {
		// TODO Auto-generated method stub
		OrderRecipients orderRecipients = orderRecipientsMapper.findOrderRecipientsByChecked().get(0);
		SubmitOrders submitOrders = new SubmitOrders();
		
		submitOrders.setOrder_user(userId);
		submitOrders.setOrder_address(orderRecipients.getRecipients_address());
		submitOrders.setOrder_phone(orderRecipients.getRecipients_phone());
		submitOrders.setOrder_name(orderRecipients.getRecipients_name());
		submitOrders.setOrder_status(1);
		submitOrders.setOrder_price(totalprice);
		submitOrders.setOrder_time(new Timestamp(System.currentTimeMillis()));
		
		submitOrderMapper.insertOrders(submitOrders);
		
		List<CartGoods> cartGoodsList = cartgoodsMapper.findAll();
		for(int i=0;i<cartGoodsList.size();i++) {
			SubmitOrderDetail submitOrderDetail = new SubmitOrderDetail();
			
			submitOrderDetail.setDetail_order(submitOrderMapper.findLastOrder());//
			submitOrderDetail.setDetail_goods(cartGoodsList.get(i).getGoods_id());
			submitOrderDetail.setDetail_price(cartGoodsList.get(i).getGoods_price());
			submitOrderDetail.setDetail_num(cartGoodsList.get(i).getNum());
			
			submitOrderDetailMapper.insertOrderDetail(submitOrderDetail);
		}
			
		return 0;
	}

	@Override
	public void updateChecked() {

		int i = orderRecipientsMapper.resetChecked();
		//int row = orderRecipientsMapper.updateChecked(recipients_id);
		
	}
}
